<?php

class sendSMS 
{
	private $secure_secret	= 'muachungsms';
	private $url_send_sms	= 'http://kannel.todo.vn/receiveSMS.php';
	private $url_send_sms_brandName	= 'http://210.245.42.38/brand_name/api_muachung.php?username=muachung&password=OvwTE2bBIhJASvrnwkWh&brandname=MuaChung';
	
	public function send($phone = '', $text = '', $kannel = 0){
		if(ONLY_BRANDNAME){
			$kannel = 0;
		}
		if(BRANDNAME_SMS && !$kannel){
			return $this->send_brandname($phone, $text);
		}
		else{
			return $this->send_kannel($phone, $text);
		}
	}
	public function send_kannel($phone = '', $text = ''){
		if($phone != ''){
			$phone = str_replace(array(' ','.',',','+'), '', $phone);
			$listValidPhone = array('09','012','016','0188','0199','849','8412','8416','84188','84199');
			$isNum  = false;
			foreach($listValidPhone as $v){
				$num = strlen($v);
				$checkNum = substr($phone,0,$num);
				if(in_array($checkNum,$listValidPhone) === true){
					$isNum = true;
				}
			}
			if(!$isNum){
				return('invalid number');
			}
			if(strlen($phone) >= 10 && strlen($phone) <= 13){
				$param = array(	'to'			=>	$phone,
								'text'			=>	rawurlencode($text)
								);
				
				if(!empty($param)){
					$param = $this->encodeUrl($param);
					include_once(ROOT_PATH.'/core/Curl.php');
					$curl = new CURL();
					$obj = $curl->post($this->url_send_sms,$param);
					if($obj == '0: Accepted for delivery'){
						return 'success';
					}
					else{
						DB::insert(T_SMS_ERR, array('phone' => $phone,'content'=>$text,'error_text'=>$obj,'type'=>'kannel','site'=>$_SERVER['HTTP_HOST'],'created'=>time()));
					}
					return ($obj);
				}
				else{
					return('wrong parram');
				}
			}
			else{
				return('invalid number');
			}
		}
		else{
			return false;
		}
	}
	
	public function send_brandname($phone = '', $text = ''){
		if($phone != ''){
			$phone = str_replace(array(' ','.',',','+'), '', $phone);
			$listValidPhone = array('09'=>'849','012'=>'8412','016'=>'8416','849'=>'849','0188'=>'84188','0199'=>'84199','8412'=>'8412','8416'=>'8416','84188'=>'84188','84199'=>'84199');
			$isNum  = false;
			$phoneNew = '';
			foreach($listValidPhone as $k=>$v){
				$num = strlen($k);
				$checkNum = substr($phone,0,$num);
				if($k == $checkNum){
					$isNum = true;
					$phoneNew = substr($phone,$num,strlen($phone));
					$phoneNew = $listValidPhone[$checkNum].$phoneNew;
					break;
				}
			}
			if(!$isNum){
				return('invalid number');
			}
			if(strlen($phoneNew) >= 10 && strlen($phoneNew) <= 13){
				$param = '&receive='.$phoneNew.'&content='.rawurlencode($text);
				include_once(ROOT_PATH.'/core/Curl.php');
				$curl = new CURL();
				$url = $this->url_send_sms_brandName.$param;
				$obj = $curl->get($url);
				$return = substr($obj,0,4);
				if($return == '1109'){
					return 'success';
				}
				else{
					DB::insert(T_SMS_ERR, array('phone' => $phone,'content'=>$text,'error_text'=>$return,'type'=>'brandname','site'=>$_SERVER['HTTP_HOST'],'created'=>time()));
				}
				return substr($obj,5,strlen($obj));
			}
			else{
				return('invalid number');
			}
		}
		else{
			return false;
		}
	}
	
	private function encodeUrl($param = array()){
		if(!empty($param)){
			$md5HashData = $this->secure_secret;
			foreach($param as $key => $value) {
				if (strlen($value) > 0) {
					$md5HashData .= $value;
				}
			}
			$param['secure_code'] = md5($md5HashData);
			
			return $param;
		}
		return false;
	}
	
}
?>